package cn.iocoder.yudao.framework.pay.core.client.impl.pax;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.model.PaxCreatePrePayOrderRequest;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.model.PaxCreatePrePayOrderResponse;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.model.PaxResult;
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
 * @author cai
 * @date 2024/05/07 12:42
 */
public class PaxQrClient extends AbstractPaxClient {

    public PaxQrClient(Long channelId, String channelCode, PaxClientConfig config) {
        super(channelId, channelCode, config);
    }

    @Override
    protected void buildCreatePrePayOrderRequest(PayOrderUnifiedReqDTO reqDTO, PaxCreatePrePayOrderRequest request) {
        super.buildCreatePrePayOrderRequest(reqDTO, request);
        if (StringUtils.isNotBlank(reqDTO.getReturnUrl())) {
            PaxCreatePrePayOrderRequest.WebExpend expend = new PaxCreatePrePayOrderRequest.WebExpend();
            expend.setReturnUrl(reqDTO.getReturnUrl());
            request.setExpend(JsonUtils.toJsonString(expend));
        }
    }

    @NotNull
    @Override
    protected PayOrderRespDTO buildSuccessPayOrderRespDTO(PayOrderUnifiedReqDTO reqDTO, PaxResult<PaxCreatePrePayOrderResponse> result) {
        String displayMode = PayOrderDisplayModeEnum.QR_CODE.getMode();
        return PayOrderRespDTO.waitingOf(displayMode, result.getData().getQrContent(),
            reqDTO.getOutTradeNo(), result);
    }

}
